<?php

namespace App\Models;

use App\Models\Traits\GoldCoinsTrait;
use App\Models\Traits\HasScopeInquire;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * App\Models\GoldCoin
 *
 * @property int $id
 * @property int $user_id
 * @property string|null $origin
 * @property string|null $result
 * @property string|null $money
 * @property string|null $remark
 * @property string|null $reason
 * @property string|null $data
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\User|null $user
 * @method static \Database\Factories\GoldCoinFactory factory(...$parameters)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin query()
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereData($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereOrigin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereReason($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereRemark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereResult($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|GoldCoin whereUserId($value)
 * @mixin \Eloquent
 * @method static Builder|GoldCoin index($scope = null)
 * @method static Builder|GoldCoin directPrize()
 * @method static Builder|GoldCoin goldCharge()
 * @method static Builder|GoldCoin inquire($condition = null)
 * @method static Builder|GoldCoin orderRefund()
 * @method static Builder|GoldCoin payGoldRecord()
 * @method static Builder|GoldCoin payOrder()
 * @method static Builder|GoldCoin resoldCost()
 * @method static Builder|GoldCoin resoldProfit()
 * @method static Builder|GoldCoin teamPrize()
 * @method static Builder|GoldCoin transferedIn()
 * @method static Builder|GoldCoin transferedOut()
 * @method static Builder|GoldCoin withdrawCharge()
 * @method static Builder|GoldCoin withdrawPrincipal()
 * @method static Builder|GoldCoin splitIncome()
 * @method static Builder|GoldCoin netProfit()
 * @method static Builder|GoldCoin resoldBeans()
 * @method static Builder|GoldCoin exceptionalReturn()
 * @method static Builder|GoldCoin unlockedGoldSubsidies()
 */
class GoldCoin extends Model
{
    use HasFactory, HasScopeInquire, GoldCoinsTrait;

    protected $guarded = ['id'];

	protected $casts = [
		'data' => 'json'
	];

	static array $inquireConditions = [
		'gold_charge' => '充值',
		'transfered_in' => '转入',
		'transfered_out' => '转出',
		'resold_cost' => '寄卖本金回款',
		'resold_profit' => '寄卖利润',
		'resold_beans' => '寄卖金豆变现',
		'direct_prize' => '推荐奖励收入',
		'team_prize' => '炼金师奖励',
		'split_income' => '置换补贴订单收入',
		'pay_gold_record' => '掌上金店订单支出',
		'pay_order' => '普通商品订单支出',
		'order_refund' => '商品订单退款',
		'withdraw_principal' => '提现',
		'net_profit' => '寄卖利润+推荐奖励+炼金师奖励',
		'unlocked_gold_subsidies'=> '解锁补贴金',
		'pay_charge' => '充值支出',
		'support_prize_coins'=> '直推奖励扶持金',
		'support_team_coins'=> '团队奖励扶持金'
	];

	public function scopeSupportPrizeCoins(Builder $builder): Builder
	{
		return $builder->where('remark', 'support_prize_coins');
	}

	public function scopeSupportTeamCoins(Builder $builder): Builder
	{
		return $builder->where('remark', 'support_team_coins');
	}

	public function scopeGoldCharge(Builder $builder): Builder
	{
		return $builder->where('remark', 'gold_charge');
	}

	public function scopeTransferedIn(Builder $builder): Builder
	{
		return $builder->where('remark', 'transfered_in_coins');
	}

	public function scopeTransferedOut(Builder $builder): Builder
	{
		return $builder->where('remark', 'transfered_out_coins');
	}

	public function scopePayCharge(Builder $builder): Builder
	{
		return $builder->where('remark', 'pay_charge');
	}

	public function scopeResoldCost(Builder $builder): Builder
	{
		return $builder->where('remark', 'resold_cost_coins');
	}

	public function scopeResoldProfit(Builder $builder): Builder
	{
		return $builder->where('remark', 'resold_profit_coins');
	}

	public function scopeResoldBeans(Builder $builder): Builder
	{
		return $builder->where('remark', 'resold_beans_coins');
	}

	public function scopeDirectPrize(Builder $builder): Builder
	{
		return $builder->where('remark', 'gold_record_direct_prize');
	}

	public function scopeTeamPrize(Builder $builder): Builder
	{
		return $builder->where('remark', 'gold_record_team_prize');
	}

	public function scopeSplitIncome(Builder $builder): Builder
	{
		return $builder->where('remark', 'split_return_coins');
	}

	public function scopePayGoldRecord(Builder $builder): Builder
	{
		return $builder->where('remark', 'pay_gold_record');
	}

	public function scopePayOrder(Builder $builder): Builder
	{
		return $builder->where('remark', 'pay_order');
	}

	public function scopeOrderRefund(Builder $builder): Builder
	{
		return $builder->where('remark', 'order_refund');
	}

	public function scopeWithdrawPrincipal(Builder $builder): Builder
	{
		return $builder->where('remark', 'withdraw_principal');
	}

	public function scopeWithdrawCharge(Builder $builder): Builder
	{
		return $builder->where('remark', 'withdraw_charge');
	}

	public function scopeExceptionalReturn(Builder $builder): Builder
	{
		return $builder->where('remark', 'prize_exceptional_return');
	}

	public function scopeNetProfit(Builder $builder): Builder
	{
		 return $builder->where(function (Builder $query) {
			    $query->orWhere('remark', 'resold_profit_coins')
				   	->orWhere('remark','gold_record_direct_prize')
				    ->orWhere('remark','gold_record_team_prize');
		    });
	}

	public function scopeUnlockedGoldSubsidies(Builder $builder): Builder
	{
		return $builder->where('remark', 'unlocked_gold_subsidies');
	}

    public function user(): BelongsTo
    {
    	return $this->belongsTo(User::class);
    }

    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }
}
